/*
 * gui_server_callback_handler.cpp
 *
 *  Created on: Jun 7, 2011
 *      Author: jirka
 */

#include <iostream>
#include "servercallbackhandlerguiimpl.h"
#include "serverservice.h"
#include "gui/login_dialog.h"
#include "gui/screencasting_window.h"
#include "gui/main_window.h"

ServerCallbackHandlerGuiImpl& ServerCallbackHandlerGuiImpl::getInstance() {
	static ServerCallbackHandlerGuiImpl instance; // guaranteed to be destroyed, instantiated on first use.
	return instance;
}

ServerCallbackHandlerGuiImpl::ServerCallbackHandlerGuiImpl()
	: logger_(log4cxx::Logger::getLogger("client.server_callback_handler")) {
}

void ServerCallbackHandlerGuiImpl::handleLoginRequest(User* user) {
	if (user) {
		LOG4CXX_INFO(logger_, "user successfully connected");
		Glib::signal_idle().connect_once(sigc::mem_fun(*loginDialog_, &LoginDialog::onLoginSuccessfull));
		Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(*mainWindow_, &MainWindow::onLoginSuccessfull), user));
		Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(*screencastingWindow_, &ScreencastingWindow::setUserName), user->getName()));
		LOG4CXX_INFO(logger_, "obtaining courses and lessons list");
		ServerService::getInstance().findAllShows();
	} else {
		LOG4CXX_WARN(logger_, "user doesn't exist on the server side");
		loginDialog_->onLoginUnsuccessfull();
	}
}

void ServerCallbackHandlerGuiImpl::handleStartScreencast(Lesson* lesson) {
	LOG4CXX_DEBUG(logger_, "handling start screencast request");

	Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(*screencastingWindow_, &ScreencastingWindow::setCapturedLesson), lesson));
	Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(*mainWindow_, &MainWindow::setCapturedLesson), lesson));
	Glib::signal_idle().connect_once(sigc::mem_fun(*screencastingWindow_, &ScreencastingWindow::play));
	Glib::signal_idle().connect_once(sigc::mem_fun(*screencastingWindow_, &ScreencastingWindow::show));
}

void ServerCallbackHandlerGuiImpl::handleStopScreencast(Lesson* lesson) {
	LOG4CXX_DEBUG(logger_, "handling stop screencast request");
	Glib::signal_idle().connect_once(sigc::mem_fun(*screencastingWindow_, &ScreencastingWindow::stop));
	Glib::signal_idle().connect_once(sigc::mem_fun(*screencastingWindow_, &ScreencastingWindow::hide));
}


void ServerCallbackHandlerGuiImpl::handleFindShowsRequest(std::list<Course*> * courses,std::list<Lesson*> * lessons) {
	LOG4CXX_DEBUG(logger_, "handling find shows request");
	Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(*mainWindow_, &MainWindow::onShowsListRequested), courses, lessons));
}

void ServerCallbackHandlerGuiImpl::handleScreencastData(std::vector<char> data,int pts) {
	LOG4CXX_DEBUG(logger_, "handling screencast data! " << pts);
	Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(screencastingWindow_, &ScreencastingWindow::enqueueVideoPacket), data, pts));
}

void ServerCallbackHandlerGuiImpl::handleScreencastAudioData(std::vector<char> data,int pts) {
	LOG4CXX_DEBUG(logger_, "handling screencast audio data!");
	Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(screencastingWindow_, &ScreencastingWindow::enqueueAudioPacket), data, pts));
}

void ServerCallbackHandlerGuiImpl::handleScreencastNotAvailable(Lesson* lesson) {
	LOG4CXX_DEBUG(logger_, "handling screencast failed!");
	Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(*mainWindow_, &MainWindow::lessonNotAvailable),lesson));
}

void ServerCallbackHandlerGuiImpl::handleChatMessage(ChatMessage* msg) {
  LOG4CXX_DEBUG(logger_, "handling chat message");
  Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(*screencastingWindow_, &ScreencastingWindow::displayChatMessage), msg));
}
